package com.mall.service.user;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.user.CollectionService;
import com.mall.custom.dao.read.CollectionReadMapper;
import com.mall.custom.dao.write.CollectionWriteMapper;
import com.mall.pojo.Collection;
import com.mall.pojo.CollectionExample;
import com.mall.pojo.CollectionExample.Criteria;
import com.mall.query.collection.CollectionQuery;


@Service
public class CollectionServiceImpl implements CollectionService {
	
	@Autowired
	private CollectionReadMapper collectionReadMapper;
	@Autowired
	private CollectionWriteMapper collectionWriteMapper;


	@Override
	public List<Collection> getCollectionByExample(CollectionExample example) {
		
		return this.collectionReadMapper.selectByExample(example);
	}

	@Override
	public int insertCollection(Collection collection) {
		collection.setGmtCreate(new Date());
		collection.setGmtModify(new Date());
		return this.collectionWriteMapper.insert(collection);
		
	}

	@Override
	public Collection getCollectionInfoByExample(CollectionExample example) {
		List<Collection> collectionList = this.collectionReadMapper.selectByExample(example);
		if(collectionList.size()<=0 || collectionList==null){
			return null;
		}
		return collectionList.get(0);
	}

	@Override
	public Collection getCollectionById(Long id) {
		if(id == null || id <= 0){
			return null;
		}
		return this.collectionReadMapper.selectByPrimaryKey(id);
	}

	@Override
	public int modifyCollection(Collection collection) {
		collection.setGmtModify(new Date());
		return this.collectionWriteMapper.updateByPrimaryKeySelective(collection);
		
	}

	@Override
	public int getCollectionCountByExample(CollectionExample example) {
		
		return this.collectionReadMapper.countByExample(example);
	}

	@Override
	public Collection getCollectionByCollectionQuery(CollectionQuery query) {
		CollectionExample collectionExample = new CollectionExample();
		Criteria criteria = collectionExample.createCriteria();
		if(query.getMallId() != null && query.getMallId() > 0){
			criteria.andMallIdEqualTo(query.getMallId());
		}
		if(query.getOnlyKey() != null && query.getOnlyKey() > 0){
			criteria.andCommOnlyKeyEqualTo(query.getOnlyKey());
		}
		if(query.getStatus() != null ){
			criteria.andStatusEqualTo(query.getStatus());
		}
		if(query.getType() != null && query.getType() > 0){
			criteria.andTypeEqualTo(query.getType());
		}
		if(query.getUserId() != null && query.getUserId() > 0){
			criteria.andUserIdEqualTo(query.getUserId());
		}
		List<Collection> collectionList = this.collectionReadMapper.selectByExample(collectionExample);
		if(collectionList == null || collectionList.size() <= 0){
			return null;
		}
		return collectionList.get(0);
	}

/*	@Override
	public Collection getCollectionByUser(Long mallId, Long userId, Integer status, Integer onlyKey) {
		if(userId == null || userId <= 0){
			return null;
		}
		CollectionExample collectionExample = new CollectionExample();
		Criteria criteria = collectionExample.createCriteria();
		if(mallId != null && mallId > 0){
			criteria.andMallIdEqualTo(mallId);
		}
		if(userId != null && userId > 0){
			criteria.andUserIdEqualTo(userId);
		}
		if(status != null && status > 0){
			criteria.andStatusEqualTo(status);
		}
		if(onlyKey != null && onlyKey > 0){
			criteria.andCommOnlyKeyEqualTo(onlyKey);
		}
		return null;
	}*/

}
